home *** CD-ROM | disk | FTP | other *** search
- From: LittleGuyRascal@msn.com (Gregory Saxton)
- Subject: RE: Will it be auto-deleted?
- Date: 27 Feb 96 03:58:17 -0800
- References: <1996Feb20.110314.46035@yogi.urz.unibas.ch>
- Message-ID: <00001a81+0000a898@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c++
- Organization: The Microsoft Network (msn.com)
-
- You are half right.
-
- Since mypointer is an automatic variable in function myfunction it
- will be deleted from the stack when the function exits.
-
- The array of 100 elements of type double allocated via operator new
- will remain on the heap until that magic moment when someone issues a
- delete [] mypointer;
-
- There is a bonus problem here. Since the value mypointer is lost
- when the function exits you have a memory leak which may exhaust the
- heap depending on the usage of this function.
-
- Copy the below statement to a post-it and put it on your monitor!
- -- If I allocate memory via operator new I must delete it - or someone must --
-
- Suggestions
-
- - Make the pointer static so the value remains intact during
- subsequent function calls to myfunction - just don't allocated the
- array each time the function is called.
-
- - Make the pointer global and allocate the data somewhere else, and
- delete it somewhere else, like in a class constructor and destructor.
-
- enjoy...
-